package com.zhiyou100.xml.jsoup;

import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import cn.wanghaomiao.xpath.model.JXNode;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class Demo5 {

    public static void main(String[] args) throws IOException, XpathSyntaxErrorException {

        //1.获取student.xml的path
        String path = Demo5.class.getClassLoader().getResource("test.xml").getPath();
        //2.获取Document对象
        Document document = Jsoup.parse(new File(path), "utf-8");
        //3.根据document对象，创建JXDocument对象
        JXDocument jxDocument = new JXDocument(document);

        //选择节点（标签）
        //获取所有的user节点
        List<JXNode> nodes = jxDocument.selN("//user");
        for (JXNode node : nodes) {
            String text = node.getElement().text();
            System.out.println("text = " + text);
        }
        //获取user节点下的username节点
        List<JXNode> jxNodes = jxDocument.selN("//user/username");
        for (JXNode jxNode : jxNodes) {
            String text = jxNode.getElement().text();
            System.out.println("text = " + text);
        }
        //获取id=001的user节点下的age节点
        List<JXNode> jxNodes1 = jxDocument.selN("//user[@id='001']/age");
        for (JXNode jxNode : jxNodes1) {

            String text = jxNode.getElement().text();
            System.out.println("age = " + text);
        }
    }
}
